package cn.edu.xmu.orderpayment.payment.mq;

import cn.edu.xmu.orderpayment.payment.constants.Constants;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Service
public class RocketMQService {
    private static final Logger logger = LoggerFactory.getLogger(RocketMQService.class);

        @Resource
        private RocketMQTemplate rocketMQTemplate;


    // 支付成功通知订单
    public void sendSuccessOrderPayMessage(String orderSn) {
        logger.info("sendSuccessOrderPayMessage: send message orderSn = " + orderSn + " delay = 0" + " time =" + LocalDateTime.now());
        rocketMQTemplate.sendOneWay("order-pay-topic2",MessageBuilder.withPayload(orderSn).build());
    }

    // 退款成功通知订单
    public void sendSuccessOrderRefundMessage(String orderSn){
        logger.info(LocalDateTime.now()+"：延迟检查的结果：退款成功，现在去通知订单"+orderSn+"修改订单退款状态");
        logger.info("sendSuccessOrderRefundMessage: send message orderSn = " + orderSn + " delay = 0" + " time =" + LocalDateTime.now());
        rocketMQTemplate.sendOneWay("order-refund-topic2",MessageBuilder.withPayload(orderSn).build());
    }

    // 支付成功通知售后
    public void sendSuccessServicePayMessage(String serviceSn){
        logger.info("sendSuccessServicePayMessage: send message serviceSn = " + serviceSn + " delay = 0" + " time =" + LocalDateTime.now());
        rocketMQTemplate.sendOneWay("service-pay-topic2",MessageBuilder.withPayload(serviceSn).build());
    }

    // 发送定时消息检查是否支付成功
    public void sendPayCheckMessage(String payment, Integer delay) {
        logger.info("sendPayCheckMessage: send message payment = " + payment + " delay =" + delay + " time =" + LocalDateTime.now());
        rocketMQTemplate.sendOneWay("pay-check-topic2", MessageBuilder.withPayload(payment).build());
    }

    // 发送定时消息检查是否退款成功
    public void sendRefundCheckMessage(String refund, Integer delay) {
        logger.info("sendPayCheckMessage: send message refund = " + refund + " delay =" + delay + " time =" + LocalDateTime.now());
        rocketMQTemplate.sendOneWay("refund-check-topic2", MessageBuilder.withPayload(refund).build());
    }

    // 发送退款成功消息给售后
    public void sendSuccessRefundMessage(String serviceSn) {
        logger.info("sendSuccessRefundMessage: send message serviceSn:" + serviceSn + " delay = 0" + " time =" + LocalDateTime.now());
        rocketMQTemplate.asyncSend("service-refund-topic2", MessageBuilder.withPayload(serviceSn).build(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("sendSuccessRefundMessage: onSuccess result = " + sendResult + " time =" + LocalDateTime.now());
            }

            @Override
            public void onException(Throwable throwable) {
                logger.info("sendSuccessRefundMessage: onException e = " + throwable.getMessage() + " time =" + LocalDateTime.now());

            }
        }, Constants.OUT_TIME * 1000);
    }

}
